package com.example.peibei.ui.weight;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.peibei.R;
import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.List;

public class SelectDateDialog extends AlertDialog.Builder {

    OnDialogSelectPositionListener onDialogSelectPositionListener;

    public SelectDateDialog(Context context) {
        super(context, R.style.Theme_dialog);
    }

    public void setOnDialogSelectPositionListener(OnDialogSelectPositionListener onDialogSelectPositionListener) {
        this.onDialogSelectPositionListener = onDialogSelectPositionListener;
    }

    public void createShow(List<String> list, List<String> list2) {
        if (list.isEmpty()){
            return;
        }
        AlertDialog dialog = create();
        dialog.show();
        Window window = dialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.BOTTOM);
        window.setAttributes(attributes);
        setView(dialog, list,list2);
    }

    private void setView(AlertDialog dialog, List<String> list, List<String> list2) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_select_date, null, false);
        WheelView wheelview = view.findViewById(R.id.wheelview);
        WheelView wheelview2 = view.findViewById(R.id.wheelview2);

        wheelview.setWheelAdapter(new ArrayWheelAdapter(getContext()));
        wheelview.setWheelData(list);
        wheelview.setWheelSize(7);

        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.BLACK;
        style.textSize = 14;
        style.textColor = Color.parseColor("#80000000");
        style.selectedTextSize = 14;
        wheelview.setStyle(style);
        if (list2 == null){
            wheelview2.setVisibility(View.GONE);
        }else {
            wheelview2.setWheelAdapter(new ArrayWheelAdapter(getContext()));
            wheelview2.setWheelData(list2);
            wheelview2.setWheelSize(7);
            wheelview2.setStyle(style);
        }

        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (onDialogSelectPositionListener != null){
                    int p = -1;
                    if (list2 != null){
                        p = wheelview2.getCurrentPosition();
                    }
                    onDialogSelectPositionListener.onSelect(wheelview.getCurrentPosition(),p);
                }
            }
        });
        dialog.setContentView(view);

    }
}
